package main;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class WcGUI {
    /**{
     * 创建并显示GUI。出于线程安全的考虑，
     * 这个方法在事件调用线程中调用。
     */
    //创建一个文本方便观看输出结果
    private TextArea result = new TextArea();
   /* 创建三个变量判断字符数、单词数、行数按钮的点击*/
    int zifushu = 0;
    int dancishu = 0;
    int hangshu = 0;

    public void createAndShowGUI() {
        // 确保一个漂亮的外观风格
        JFrame.setDefaultLookAndFeelDecorated(true);


        // 创建及设置窗口
        JFrame frame = new JFrame("WC");
        JButton open = new JButton("open");

          //给open按钮设置监听事件
        open.addActionListener(new FileChooseListener());

        frame.setLayout(new BoxLayout(frame,BoxLayout.Y_AXIS));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        // 创建复选框
        JCheckBox checkBox01 = new JCheckBox("字符数");
        JCheckBox checkBox02 = new JCheckBox("单词数");
        JCheckBox checkBox03 = new JCheckBox("行数");


        checkBox01.addActionListener(new OneListener());
        checkBox02.addActionListener(new TwoListener());
        checkBox03.addActionListener(new ThreeListener());

        Box topbox = Box.createHorizontalBox();
        Box midbox = Box.createVerticalBox();
        topbox.add(checkBox01);
        topbox.add(checkBox02);
        topbox.add(checkBox03);
        midbox.add(result);
        midbox.add(open);

        /*panel.add(checkBox01);
        panel.add(checkBox02);
        panel.add(checkBox03);
        panel.add(checkBox04);
        panel.add(checkBox05);*/
        panel.add(topbox);
        panel.add(midbox);

        frame.setContentPane(panel);

        // 添加 标签
        JLabel label = new JLabel("先勾选功能再open文件");
        frame.getContentPane().add(label);

        // 显示窗口
        frame.pack();
        frame.setBounds(50,50,500,500);
        frame.setVisible(true);
    }
    //文件选择窗口的创建
    class FileChooseListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JFileChooser jfc=new JFileChooser();
            jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
            jfc.showDialog(new JLabel(), "选择");
            File file=jfc.getSelectedFile();//得到文件
            if(file.isDirectory()){
                System.out.println("文件夹:"+file.getAbsolutePath());
            }else if(file.isFile()){
                System.out.println("文件:"+file.getAbsolutePath());
            }
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {

                String s="" ;
                int a=0;//字符数
                int b=0;//行数
                int c=0;//单词数

                while ((s = br.readLine())!=null){

                    b++;
                    a+=s.length();
                     c+=s.split("\\s+").length;
                }
                if (zifushu==1){
                result.append("字符数:"+a);}
                if (dancishu==1){
                    result.append(("  单词数:"+c));
                }
                if (hangshu==1){
                    result.append("  行数："+b);
                }
                if (zifushu==0&&dancishu==0&&hangshu==0){
                    result.append("输出为空,请选择功能");
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    //三个复选框的监听事件
    class OneListener implements ActionListener{
        public void actionPerformed(ActionEvent event)
        {
            JCheckBox checkBox = (JCheckBox) event.getSource();
            System.out.println(checkBox.getText() + " 是否选中: " + checkBox.isSelected());
            if (checkBox.isSelected()==true)
            {zifushu=1;}
            else {
                zifushu=0;
            }



        }
    }
    class  TwoListener implements  ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JCheckBox checkBox = (JCheckBox) e.getSource();
            System.out.println(checkBox.getText() + " 是否选中: " + checkBox.isSelected());
            if (checkBox.isSelected()==true) {
                dancishu = 1;
            }
            else{
                dancishu = 0;
            }
        }
    }
    class  ThreeListener implements  ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            JCheckBox checkBox = (JCheckBox) e.getSource();
            System.out.println(checkBox.getText() + " 是否选中: " + checkBox.isSelected());
            if (checkBox.isSelected()==true){
            hangshu=1;}
            else{
                hangshu = 0;
            }
        }
    }

    public static void main(String[] args) {
        // 显示应用 GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                WcGUI wcGUI=new WcGUI();
                wcGUI.createAndShowGUI();
            }
        });
    }
}
